// 只需修改obj对象内相对应的参数即可，本页面需要用到的元素只有四个
// 1、存放职位名称
// 2、存放职位详情
// 3、申请按钮
// 4、返回职位列表按钮
// ps: 改class名后，相应的css样式选择器需要修改
$(function () {
  // 获取网址url
  var url = window.location;
  var Isapply = GetParam(url, "apply");
  // 需要修改的内容容器
  var obj = {
    // 添加存放职位名称class名
    position: ".job-title",
    // 添加存放职位详情的class名
    des: ".desc",
    // 立即申请按钮class名
    go: ".apply",
    // 返回职位列表按钮class名
    back: ".back",
    // 从url处获得的传参数据，不用修改，直接获取上一个页面传来的参数
    jobid: GetParam(url, "jobid")
  };
  // 匿名函数自执行
  !(function () {
    var $position = $(obj.position);
    var $des = $(obj.des);
    var $go = $(obj.go);
    var $back = $(obj.back);
    coapi.getJobDetail(obj.jobid, function (data) {
      console.log(data);
      $position.text(data.resultbody.jobname);
      $des.html(data.resultbody.jobinfo);
      bold();
    });
    // 立即申请
    $go.on("click", function (event) {
      event.preventDefault();
      $.ajax({
        url: "https://i.51job.com/delivery/platform/delivery_api.php?jobid=" +
          obj.jobid,
        type: "get",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "jobCallback",
        success: function (data) {
          var imgsrc = "";
          var tips = "";
          var $tipImg = $(".tipImg");
          var $tips = $(".tips");
          switch (data.status) {
            case "1":
              //tips = '申请成功！请勿重复投递';
              console.log(data);
              switch (data.result[obj.jobid].status) {
                case "1":
                  imgsrc = "images/true.png";
                  tips = "投递成功，七天内请勿重复投递";
                  console.log("投递成功");
                  break;
                case "-100":
                  imgsrc = "images/false.png";
                  tips = "职位过期，请重现选择职位";
                  console.log("职位过期");
                  break;
                case "-101":
                  imgsrc = "images/false.png";
                  tips = "七天内重复投递";
                  console.log("七天内重复投递");
                  break;
                case "-102":
                  imgsrc = "images/false.png";
                  tips = "特殊职位，2s跳转投递页面";

                  var timer = setTimeout(function () {
                    window.location.href = data.result[obj.jobid].result;
                    clearTimeout(timer);
                  }, 2000);
                  console.log("特殊职位102，2s跳转投递页面");
                  break;
                case "-103":
                  imgsrc = "images/false.png";
                  tips = "特殊职位，2s跳转投递页面";
                  var timer = setTimeout(function () {
                    window.location.href = data.result[obj.jobid].result;
                    clearTimeout(timer);
                  }, 2000);
                  console.log("特殊职位103，2s跳转投递页面");
                  break;

                  // PS：需要注意的是，若返回-102或者-103则为特殊职位，需要根据返回的result进行跳转。
              }

              break;
            case "-1":
              tips = "职位传参不合法!";
              // window.open(data.result);
              break;
            case "-2":
              tips = "未登录！ 2s后跳入登陆界面";
              imgsrc = "images/false.png";
              var timer = setTimeout(function () {
                window.location.href =
                  data.result + "?url=" + url + "%26apply=yes";
                clearTimeout(timer);
              }, 2000);
              break;
            case "-3":
              tips = "简历信息不合法！2s后进入修改界面";
              imgsrc = "images/false.png";
              var timer = setTimeout(function () {
                window.location.href = data.result;
                clearTimeout(timer);
              }, 2000);
              break;
            case "-4":
              tips = "简历不完整！2s后进入填写界面";
              imgsrc = "images/false.png";
              var timer = setTimeout(function () {
                window.location.href = data.result;
                clearTimeout(timer);
              }, 2000);
              break;
            case "-5":
              tips = "无简历！2s后进入填写界面";
              imgsrc = "images/false.png";
              var timer = setTimeout(function () {
                window.location.href = data.result;
                clearTimeout(timer);
              }, 2000);
              break;
            case "-6":
              tips = "职位数大于50！";
              imgsrc = "images/false.png";
              var timer = setTimeout(function () {
                //window.open(data.result);
                clearTimeout(timer);
              }, 2000);
              break;
            default:
              break;
          }

          $(".tipImg").attr("src", imgsrc);
          $(".tips").text(tips);
          $(".job3").fadeIn();

          var timer2 = setTimeout(function () {
            $(".job3").fadeOut();
            clearTimeout(timer2);
          }, 6000);
        },
        error: function (data) {}
      });
      // window.location.href = "job4.html?jobid="+obj.jobid;
    });

    // 返回职位列表
    $back.on("click", function (event) {
      event.preventDefault();
      window.location.href = "society.html";
    });

    $(".job3").click(function () {
      $(this).hide();
    });
  })();

  if (Isapply === "yes") {
    $(".go").trigger("click");
  }
  // 如果p标签内容没有数字序号或#或.或、，就给字体加粗
  // 相当于给 “任职要求：”、“岗位职责：”等加粗
  function bold() {
    var reg = /(\d+|#|-|\/|•)/;
    $(obj.des)
      .find("p")
      .each(function (index, el) {
        if (!reg.test($(this).text())) {
          $(this).css({
            fontWeight: "bold",
            fontSize: "16px"
          });
        }
      });
  }

  // 根据指定参数名获取url的参数值
  function GetParam(url, id) {
    url = url + "";
    regstr = "/(\\?|\\&)" + id + "=([^\\&]+)/";
    //eval可以将 regstr字符串转换为 正则表达式
    reg = eval(regstr);
    result = url.match(reg);
    if (result && result[2]) {
      return result[2];
    }
  }

  function getUrlParam(name) {
    // 用该属性获取页面 URL 地址从问号 (?) 开始的 URL（查询部分）
    var url = window.location.search;
    // 正则筛选地址栏
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    // 匹配目标参数
    var result = url.substr(1).match(reg);
    //返回参数值
    return result ? decodeURIComponent(result[2]) : "";
  }
});

// 根据指定参数名获取url的参数值
function GetParam(url, id) {
  url = url + "";
  regstr = "/(\\?|\\&)" + id + "=([^\\&]+)/";
  //eval可以将 regstr字符串转换为 正则表达式
  reg = eval(regstr);
  result = url.match(reg);
  if (result && result[2]) {
    return result[2];
  }
}

function getUrlParam(name) {
  // 用该属性获取页面 URL 地址从问号 (?) 开始的 URL（查询部分）
  var url = window.location.search;
  // 正则筛选地址栏
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  // 匹配目标参数
  var result = url.substr(1).match(reg);
  //返回参数值
  return result ? decodeURIComponent(result[2]) : "";
}

var jobid_jobdetail = getUrlParam("jobid");
var _tkd = _tkd || [];
var param =
  "webId=4&accountId=&ctmId=131818&guid=&dType=1&jobId=" +
  jobid_jobdetail +
  "&type=&urlType=3"; //参数请对应查看日志传参

_tkd.push(["_setParams", param]);
_tkd.push(["_setAutoButtonClick", true, "a"]);
_tkd.push(["_setAutoPageview", true]);

document.write(
  unescape(
    "%3Cscript src='http://trace.51jingying.com/trackBigdata.js' type='text/javascript'%3E%3C/script%3E"
  )
);